import axios from 'axios';
import { getStorage } from './utils';

axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.timeout = 30000;
axios.interceptors.request.use(config => config, error => Promise.reject(error));
axios.interceptors.response.use(resp => resp, error => Promise.reject(error));

const userInfo = getStorage('userInfo') || {};
const time = new Date().getTime();
export default {
  post(url, params) {
    return new Promise((resolve, reject) => {
      axios.post(url, params, {
        headers: {
          systemData: JSON.stringify({
            token: userInfo.accessToken || '',
          })
        }
      }).then((res) => {
        if (!res) {
          // eslint-disable-next-line no-console
          console.error(`The request url: ${url} load fail.`);
        }
        const { data } = res || {};
        if (data.code === '000') { // 接口成功
          resolve(data);
        } else {
          reject(res);
        }
      }, err => reject(err));
    });
  },
  get(url, params) {
    return new Promise((resolve, reject) => {
      axios.get(url, {
        data: params
      }, {
        headers: { token: '' }
      }).then(response => resolve(response.data), err => reject(err));
    });
  }
};
